package com.adingxiong.pm.controller;


import com.adingxiong.pm.entity.Pmuser;
import com.adingxiong.pm.model.Result;
import com.adingxiong.pm.model.RetCode;
import com.adingxiong.pm.server.IPmuserService;
import com.adingxiong.pm.util.MD5Utils;
import com.adingxiong.pm.entity.vo.UserRoleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiongc
 * @since 2020-02-15
 */
@RestController
@RequestMapping("/pmuser")
@Api(tags = "用户管理")
public class PmuserController {


    @Autowired(required = true)
    private IPmuserService  iPmuserService;



//    @ApiOperation(value = "引导用户登录")
//    @RequestMapping(value = "/notLogin" ,method = RequestMethod.GET)
//    public void noLogin (HttpServletResponse response) throws IOException {
//        response.sendRedirect("pm/pmuser/login");
//    }
//    @ApiOperation(value = "用户拦截")
//    @RequestMapping(value = "/noAuthorize" ,method = RequestMethod.GET)
//    public String notRole(){
//        return "您没有访问改页面的权限";
//    }
//
//    @ApiOperation(value = "用户登录")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name ="name" ,value ="用户名" , paramType = "query" , required = true),
//            @ApiImplicitParam(name ="password" ,value ="密码" ,paramType ="query" ,required = true)
//    })
//    @RequestMapping(value = "/login" ,method = RequestMethod.POST)
//    public  String login(String name,String password){
//        //使用SecurityUtils  创建该用户
//        Subject subject = SecurityUtils.getSubject();
//        //使用用户的关键信息创建一个token令牌 用于验证
//        UsernamePasswordToken token = new UsernamePasswordToken(name,password);
//        //执行认证登录 ，这里会调用MyRealm中doGetAuthenticationInfo方法进行身份验证
//        subject.login(token);
//
//        //根据权限  返回指定的数据
//        return "用户认证成功";
//
//    }

    @ApiOperation(value = "关联查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page" ,value = "页码" ,paramType = "query"),
            @ApiImplicitParam(name = "size" ,value = "条数" ,paramType = "query"),
    })
    @RequestMapping(value = "/findUser"  , method = RequestMethod.GET)
    public Result getUserDetailInfo (Integer page ,Integer size){
        Page<UserRoleVo> userinfo = iPmuserService.getUserDeatilInfo(new Page<>(page ,size));
        return new Result<>().setCode(RetCode.SUCCESS).setMsg("查询成功").setData(userinfo);
    }

    @ApiOperation("模糊查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="keywords" ,value="关键字" , paramType = "query" ),
            @ApiImplicitParam(name ="page" ,value="页面" , paramType = "query" ,dataType = "int"),
            @ApiImplicitParam(name ="size" ,value="条数" , paramType = "query" ,dataType = "int"),
    })
    @RequestMapping(value = "/findBykey", method = RequestMethod.GET)
    public Result findUserBykey (String keywords , Integer page, Integer size){
        Page<UserRoleVo> pages = new Page<>();
        pages.setSize(size);
        pages.setPages(page);
        Page<UserRoleVo> userList = iPmuserService.findBykeywords(pages , keywords);
        return new Result<>().setCode(RetCode.SUCCESS).setMsg("查询成功").setData(userList);
    }


    @ApiOperation(value = "新增用户信息")
    @RequestMapping(value = "/saveUserInfo" ,method = RequestMethod.POST)
    public Result saveUserInfo(Pmuser pmuser)  {
        String pwd = pmuser.getPassword();
        String mdpwd = MD5Utils.string2MD5(pwd);
        pmuser.setPassword(mdpwd);

        boolean insert = iPmuserService.save(pmuser);
        if(insert){
            return new Result<>().setCode(RetCode.SUCCESS).setMsg("新增成功").setData(insert);
        }else {
            return new Result<>().setCode(RetCode.FAIL).setMsg("新增失败").setData(insert);
        }

    }

    @ApiOperation(value = "根据id 修改用户信息")
    @RequestMapping(value = "/updateUserInfoById" ,method = RequestMethod.POST)
    public Result updateUserInfo(Pmuser pmuser){
        if(pmuser == null){
            return new Result().setCode(RetCode.FAIL).setMsg("参数为空");
        }
        boolean update = iPmuserService.updateById(pmuser);
        if(update){
            return new Result<>().setCode(RetCode.SUCCESS).setMsg("修改成功").setData(update );
        }else {
            return new Result<>().setCode(RetCode.FAIL).setMsg("修改失败").setData(update);
        }

    }


    @ApiOperation(value = "表单唯一性检验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type" , value = "检验的类型(name,email,phone)" , dataType ="String" ,paramType = "query" ),
            @ApiImplicitParam(name = "value" , value = "检验的值" , dataType ="String" ,paramType = "query" )
    })
    @RequestMapping(value = "/checkOnly" ,method =  RequestMethod.GET)
    public Result onlyCheck (String type , String value){
        if(StringUtils.isEmpty(type) || StringUtils.isEmpty(value)){
            return  new Result().setCode(RetCode.FAIL).setMsg("参数为空");
        }
        String tip = null;
        QueryWrapper<Pmuser> queryWrapper = new QueryWrapper<>();
        switch (type) {
            case "name":
                queryWrapper.eq("name", value);
                tip = "用户名已存在";
                break;
            case "email":
                queryWrapper.eq("email", value);
                tip = "邮箱已存在";
                break;
            case "phone":
                queryWrapper.eq("phone", value);
                tip = "手机号码已存在";
                break;
            default:
        }
        Pmuser pmuser = iPmuserService.getOne(queryWrapper);
        if(pmuser ==null) {
            return new Result <>().setCode(RetCode.SUCCESS).setData(true).setMsg("校验通过");
        }
        return new Result <>().setCode(RetCode.SUCCESS).setData(false).setMsg(tip);
    }

    @ApiOperation(value = "根据id删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "用户id" ,paramType ="query")
    })
    @RequestMapping(value = "/delByid", method = RequestMethod.GET)
    public  Result delUserById (String id ){
        if(StringUtils.isEmpty(id)){
            return  new Result<>().setCode(RetCode.NOT_FOUND).setMsg("参数为空");
        }

        boolean del = iPmuserService.removeById(id);
        if(del){
            return  new Result<>().setCode(RetCode.SUCCESS).setMsg("删除成功");
        }
        return  new Result<>().setCode(RetCode.FAIL).setMsg("系统异常，请稍后重试");
    }




}

